package com.evil.Utils;

import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import java.util.Properties;

/********************************************
 * 工程名：Web
 * 包名：com.evil.Utils
 * 时间：16-9-27 下午1:25
 * 作者：白雨濃 evil
 * 邮箱：evilbai@foxmail.com
 * 内容：properties配置文件工具类
 ********************************************/
public class PropertiesUtils
{
    private static final String DATA_PROPERTIES = "data.properties";

    private Properties prop;
    private String path;
    private String propertiesName;

    public PropertiesUtils()
    {
        prop = new Properties();//属性集合对象
        path = this.getClass().getClassLoader().getResource("").getPath();
        try
        {
            path = URLDecoder.decode(path, "utf-8");
        }
        catch (UnsupportedEncodingException e)
        {
            e.printStackTrace();
        }
        FileInputStream fis = null;//属性文件流
        try
        {
            this.propertiesName = DATA_PROPERTIES;
            fis = new FileInputStream(path + DATA_PROPERTIES);
            prop.load(fis);//将属性文件流装载到Properties对象中
        }
        catch (IOException e)
        {
            e.printStackTrace();
        }
    }

    public PropertiesUtils(String propertiesName)
    {
        prop = new Properties();//属性集合对象
        path = this.getClass().getClassLoader().getResource("").getPath();
        try
        {
            path = URLDecoder.decode(path, "utf-8");
        }
        catch (UnsupportedEncodingException e)
        {
            e.printStackTrace();
        }
        FileInputStream fis = null;//属性文件流
        try
        {
            this.propertiesName = propertiesName;
            fis = new FileInputStream(path + propertiesName);
            fis.close();
            prop.load(fis);//将属性文件流装载到Properties对象中
        }
        catch (IOException e)
        {
            e.printStackTrace();
        }
    }

    public String get(String key)
    {
        return prop.getProperty(key);
    }

    public int getInt(String key)
    {
        return Integer.valueOf(prop.getProperty(key));
    }

    public String get(String key, String defaultValue)
    {
        return prop.getProperty(key, defaultValue);
    }

    public int getInt(String key, String defaultValue)
    {
        return Integer.valueOf(prop.getProperty(key, defaultValue));
    }


    /**
     * 设置变量
     *
     * @param key
     * @param value
     */
    public void set(String key, String value)
    {
        prop.setProperty(key, value);
    }

    /**
     * 提交
     *
     * @param info 注释说明
     */
    public void submit(String info)
    {
        FileOutputStream fos = null;
        try
        {
            fos = new FileOutputStream(path + "/data.properties");
            prop.store(fos, info);
            fos.close();// 关闭流
        }
        catch (IOException e)
        {
            e.printStackTrace();
        }
    }


}
